package com.keeley.spider.server;

import com.keeley.spider.model.Apk;
import com.keeley.spider.util.PropertiesUtil;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;

import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Tianjun
 * Date: 14-9-25
 * Time: 下午4:28
 * To change this template use File | Settings | File Templates.
 */
public class BaiduSpriderServer extends  SpiderServer {

    protected BaiduSpriderServer(String name) {
        super(name);
    }

    @Override
    public String getDownUrl() {
        try {
            String url = PropertiesUtil.get("baiduUrl")+ URLEncoder.encode(apkName, "utf-8");
            org.jsoup.nodes.Document doc = null;
            doc = Jsoup.parse(new URL(url), 10000);
            Elements e=doc.select(".inst-btn-big");
            if(e.isEmpty())
                e= doc.select(".little-install a");
            if(e.isEmpty())
                throw  new RuntimeException(apkName+"百度查询不到");
            String href=e.first().attr("data_url");
            return href;
        } catch (Exception e) {
            logger.error(e);
            throw  new RuntimeException(apkName+"[百度解析]失败");
        }
    }

    @Override
    public String getDownDir() {
        String date=new SimpleDateFormat("yyyyMMdd").format(new Date());
        return "F:\\temp\\百度\\"+date;
    }
}
